import java.util.Scanner;

public class NGuessNumber {
    public static int number[] = new int[4];

    public static int[] generateAnswer() {

        int index = 0;


        while (true) {
            int random = (int) (Math.random() * 9);
            int j;
            for (j = 0; j < number.length; j++) {
                if (random == number[j]) {
                    break;
                }
            }
            if (j == number.length) {
                number[index++] = random;
            }
            if (index == 4) {
                break;
            }

        }
        return number;
    }

    public static int[] gs = new int[4];

    public static int[] getPlayerInput() {
        try {
            Scanner scan = new Scanner(System.in);

            for (int i = 0; i < 4; ) {
                System.out.println("请输入大于0小于9的数字,这是第" + i + "个");
                int sc = scan.nextInt();
                if (sc >= 0 && sc <= 9) {
                    gs[i] = sc;
                    i++;
                } else {
                    gs[i] = sc;
                }
            }
        } catch (Exception e) {
            System.out.println("输入错了!");
        }
        return gs;
    }

    public static int A = 0;
    public static int B = 0;

    public static String compareGuessAnswer() {
        for (int i = 0; i < number.length; i++) {
            for (int j = 0; j < number.length; j++) {
                if (i != j) {
                    if (number[i] == gs[j]) {
                        B++;
                    }
                } else {
                    if (number[i] == gs[j]) {
                        A++;
                    }
                }
            }
        }
        System.out.println(A + "A" + B + "B");
        System.out.println("A表示存在且位置正确的数字个数，B表示存在且位置不正确的数字个数！");
        return A + "A" + B + "B\n" + "A表示存在且位置正确的数字个数，B表示存在且位置不正确的数字个数！\n" + "如：1A2B,表示你猜测的四个数字有3个是对的，有一个位置对，两个位置不对。";
    }

    public static boolean isWin() {
        boolean jg = false;
        if (A == 4) {
            jg = true;
        } else {
            jg = false;
        }
        return jg;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            generateAnswer();

            while (true) {
                getPlayerInput();
                compareGuessAnswer();
                if (isWin()) {
                    System.out.println("恭喜你，成功了！");
                    break;
                } else {
                    System.out.println("猜错了，再试一次吧！");
                }
            }
            System.out.println("在玩一次(y/n)");
            String choice = scanner.next();
            if (!choice.equalsIgnoreCase("y")) {
                break;
            }
        }
    }
}
